package com.icode.web.controller.house;

import com.icode.core.dto.house.EquipmentDTO;
import com.icode.core.model.house.EquipmentType;
import com.icode.service.house.HouseService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.servlet.ModelAndView;

import javax.servlet.http.HttpServletRequest;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * Created with IntelliJ IDEA.
 * User: SunKai
 * Date: 14-11-5
 * Time: 上午9:42
 * To change this template use File | Settings | File Templates.
 */
@Controller
@RequestMapping("/house/equipment")
public class EquipmentController {
    @Autowired
    private HouseService houseService;

    @RequestMapping(value = "form", method = RequestMethod.GET)
    public ModelAndView showForm() {
        return new ModelAndView("back/houseBase/equipment", "equipment", new EquipmentDTO());
    }

    @RequestMapping(value = "form", method = RequestMethod.POST)
    public String submitForm(EquipmentDTO equipmentDTO) {
        houseService.saveOrUpdateEquipment(equipmentDTO, EquipmentType.EQUIPMENT);
        return "redirect:/house/equipment/list";
    }

    @RequestMapping(value = "add", method = RequestMethod.POST)
    @ResponseBody
    public Map<String, Object> add(@RequestParam("name") String name) {
        Map<String, Object> modelMap = new HashMap<>(1);
        modelMap.put("success", "true");
        return modelMap;
    }

    @RequestMapping(value = "list", method = RequestMethod.GET)
    public String showEquipments(Model model) throws Exception {
        List<EquipmentDTO> equipmentDTOs = houseService.getEquipmentDTOs(EquipmentType.EQUIPMENT);
        model.addAttribute("list", equipmentDTOs);
        return "back/houseBase/equipmentOverview";
    }

    @RequestMapping(value = "update/{guid}", method = RequestMethod.GET)
    public String update(@PathVariable String guid, Model model) {
        EquipmentDTO equipmentDTO = houseService.getEquipmentByGuid(guid);
        model.addAttribute("equipment", equipmentDTO);
        return "back/houseBase/equipment";
    }

    @RequestMapping(value = "delete/{guid}", method = RequestMethod.GET)
    public String delete(@PathVariable String guid) {
        houseService.deleteEquipmentByGuid(guid);
        return "redirect:/house/equipment/list";
    }
}
